﻿using Iworld.Administrator.Model;
using Iworld.Administrator.Service.Export;
using Iworld.basis.Service;
using System;
using System.Linq;
using System.Runtime.Serialization;

namespace Iworld.Administrator.Service.Import
{
    /// <summary>
    /// 获取管理员列表
    /// </summary>
    [DataContract]
    public class GetAdministratorsImport : GetPageListCheckAdministratorImportBase
    {
        #region 属性

        /// <summary>
        /// 所从属的用户组的存储指针
        /// </summary>
        [DataMember]
        public int? GroupId { get; set; }

        /// <summary>
        /// 关键字
        /// </summary>
        [DataMember]
        public string Keyword { set; get; }
         [DataMember]
        public DateTime? BeginTime { set; get; }
         [DataMember]
        public DateTime? EndTime { set; get; }
        #endregion

        public PageResult<AdministratorExport> GetAdministrators(IModelToDbContextOfAdministrator db)
        {
            int pageSize = 10;
            int startRow = pageSize * (this.PageIndex - 1);
            IQueryable<Model.Administrator> temp = db.Administrators;
            if (Keyword != null && Keyword.Trim().Length > 0)
            {
                temp = temp.Where(x => x.Username.Contains(Keyword)
                    || x.Account.Contains(Keyword));
            }
            if (GroupId != null)
            {
                temp=temp.Where(x => x.Group.Id == (int)GroupId);
            }
            if(BeginTime!=null)
            {
                temp = temp.Where(x => x.LastLoginTime >= (DateTime)BeginTime);
            }
            if (EndTime != null)
            {
                temp = temp.Where(x => x.LastLoginTime <= (DateTime)EndTime);
            }
            int totalCount = temp.Count();
            var list = temp.OrderByDescending(x => x.CreatedTime).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
            {
                return new AdministratorExport(x,null);
            });

            return new PageResult<AdministratorExport>(this.PageIndex, totalCount, pageSize, list);
        }

    }
}
